package com.oriental.manage.service.specially.Impl;

import com.oriental.check.commons.util.BeanMapperUtil;
import com.oriental.manage.core.paging.Pagination;
import com.oriental.manage.core.response.ResponseDTO;
import com.oriental.manage.dao.specially.MerchantRiskInfoReportMapper;
import com.oriental.manage.dao.specially.PersonRiskInfoReportMapper;
import com.oriental.manage.model.specially.AreaNumber;
import com.oriental.manage.pojo.specially.MerchantRiskInfo;
import com.oriental.manage.pojo.specially.PersonRiskInfo;
import com.oriental.manage.service.specially.MerchantRiskInfoService;
import com.oriental.manage.service.specially.PersonRiskInfoService;
import com.oriental.riskfront.api.service.DataDictInterface;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by luoxin on 2017/4/20.
 */
@Slf4j
@Service
class MerchantRiskInfoServiceImpl implements MerchantRiskInfoService {

    @SuppressWarnings("SpringJavaAutowiringInspection")
    @Autowired
    private DataDictInterface dataDictInterface;
    @Autowired
    private MerchantRiskInfoReportMapper merchantRiskInfoReportMapper;

    @Override
    public void insert(MerchantRiskInfo merchantRiskInfo, ResponseDTO<String> responseDTO) {

        if (merchantRiskInfoReportMapper.insertModel(merchantRiskInfo) > 0) {
            responseDTO.setSuccess(true);
            responseDTO.setMsg("添加成功");
        } else {
            responseDTO.setSuccess(false);
            responseDTO.setMsg("添加失败");
        }
    }

    @Override
    public void delete(MerchantRiskInfo merchantRiskInfo, ResponseDTO<String> responseDTO) {
        if (merchantRiskInfoReportMapper.delete(merchantRiskInfo.getId()) > 0) {
            responseDTO.setSuccess(true);
        } else {
            responseDTO.setSuccess(false);
        }
    }

    @Override
    public void queryPage(Pagination<MerchantRiskInfo> pagination,MerchantRiskInfo merchantRiskInfo) {
        List<MerchantRiskInfo> list=merchantRiskInfoReportMapper.query(merchantRiskInfo);
        List<Map<String, Object>> dataAddress = dataDictInterface.getDataAddress("ADDRESS_Select");
        if(list!=null && list.size()>0){
            for(MerchantRiskInfo merchantRisk:list){
                {
                    String areaCode=merchantRisk.getOccurarea();
                    //String areaCode="210000,220000,210100";
                    if(areaCode!=null && !"".equals(areaCode)) {
                        String[] stringArr = areaCode.split(",");
                        List<AreaNumber> basicData = new ArrayList<>();
                        for (int i = 0; i < stringArr.length; i++) {
                            AreaNumber areaNumber = new AreaNumber();
                            for (Map<String, Object> map : dataAddress) {
                                String areaCo = (String) map.get("areaCode");
                                String areaCoSub = areaCo.substring(0, 2);
                                String areaNa = (String) map.get("areaName");
                                if (areaCo.equals(stringArr[i])) {
                                    areaNumber.setAreaCode(areaCo);
                                    areaNumber.setAreaName(areaNa);
                                    areaNumber.setCityName(areaNa);
                                    basicData.add(areaNumber);
                                } else {
                                    if (stringArr[i].length() > 1) {
                                        if (areaCoSub.equals(stringArr[i].substring(0, 2))) {
                                            areaNumber.setAreaCode(areaCo);
                                            areaNumber.setAreaName(areaNa);
                                            List<Map<String, Object>> citys = (List<Map<String, Object>>) map.get("citys");
                                            for (Map<String, Object> mapCity : citys) {
                                                String cityCo = (String) mapCity.get("cityCode");
                                                String cityNa = (String) mapCity.get("cityName");
                                                if (cityCo.equals(stringArr[i])) {
                                                    areaNumber.setCityCode(cityCo);
                                                    areaNumber.setCityName(areaNa+cityNa);
                                                }
                                            }
                                            basicData.add(areaNumber);
                                        }
                                    }
                                }
                            }
                        }
                        merchantRisk.setBasicData(basicData);
                    }
                }
            }
            pagination.setRowCount(list.get(0).getRowCount());
            pagination.setList(list);
        }
    }

    @Override
    public int queryByRiskId(String RiskId) {
        int flag=0;
        List<MerchantRiskInfo> list=merchantRiskInfoReportMapper.queryByRiskId(RiskId);
        if(list!=null && list.size()>0){
            flag=1;//存在正在处理中的失效数据
        }
        return flag;
    }

}
